package com.honeycomb.service.impl;

import java.util.List;

import com.honeycomb.db.dao.hibernate.IArticleinfoDao;
import com.honeycomb.db.entity.ArticleInfo;
import com.honeycomb.db.entity.MapInfo;
import com.honeycomb.db.entity.TagArticleRel;
import com.honeycomb.db.entity.TagArticleRelId;
import com.honeycomb.db.entity.TagInfo;
import com.honeycomb.db.entity.UserInfo;
import com.honeycomb.service.BaseService;
import com.honeycomb.service.IArticleService;
import com.honeycomb.utils.HoneyCombTools;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.ArticleinfoVO;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.MapInfoVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.TaginfoVO;
import com.honeycomb.vo.UserInfoVO;

public class ArticleServiceImpl extends BaseService implements IArticleService {
	
	/**
	 * 
	 * @author ZhongJiang
	 * 
	 * @function 添加Article Service层方法
	 * 
	 * @param userInfoVO 用户VO
	 * 
	 * @param mapInfoVO 地图VO
	 * 
	 * @return {ResultDto<BaseVO>}
	 * 
	 */
	@Override
	public ResultDto<BaseVO> doAddArticleInfo(ArticleinfoVO articleInfoVO, UserInfoVO userInfoVO, MapInfoVO mapInfoVO, List<TaginfoVO> tagInfoVOList) {
		logger.info("Hello - ArticleServiceImpl - doAddArticleInfo...");
		ResultDto<BaseVO> resultDtoAddArticle = new ResultDto<BaseVO>();
		UserInfo userInfo = null;
		MapInfo mapInfo = null;
		TagInfo tagInfo = null;
		ArticleInfo articleInfo = null;
		TagArticleRelId tagArticleRelId = null;
		TagArticleRel tagArticleRel = null;
		try {
			//获取userInfoInfo实例
			userInfo = this.getiUserinfoDao().findById(userInfoVO.getUserId());
			
			//生成mapInfo实例
			mapInfo = new MapInfo();
			mapInfo.setMapId(mapInfoVO.getMapId());
			mapInfo.setMapCreateDttm(mapInfoVO.getMapCreateDttm());
			mapInfo.setMapCreateIp(mapInfoVO.getMapCreateIp());
			mapInfo.setMapProvider(mapInfoVO.getMapProvider());
			mapInfo.setMapAddr(mapInfoVO.getMapAddr());
			mapInfo.setMapDesc(mapInfoVO.getMapDesc());
			mapInfo.setMapX(mapInfoVO.getMapX());
			mapInfo.setMapY(mapInfoVO.getMapY());
			mapInfo.setMapType(mapInfoVO.getMapType());
			this.getiMapinfoDao().add(mapInfo);
			
			//生成articleInfo实例
			articleInfo = new ArticleInfo();
			articleInfo.setArticleId(articleInfoVO.getArticleId());
			articleInfo.setArticleTitle(articleInfoVO.getArticleTitle());
			articleInfo.setArticleContent(articleInfoVO.getArticleContent());
			articleInfo.setArticleSummary(articleInfoVO.getArticleSummary());
			articleInfo.setArticleTag(articleInfoVO.getArticleTag());
			articleInfo.setArticleCreateDttm(articleInfoVO.getArticleCreateDttm());
			articleInfo.setArticleCreateIp(articleInfoVO.getArticleCreateIp());
			articleInfo.setArticleReplyCnt(articleInfoVO.getArticleReplyCnt());
			articleInfo.setArticleSeq(articleInfoVO.getArticleSeq());
			articleInfo.setArticleStatus(articleInfoVO.getArticleStatus());
			articleInfo.setArticleVisitCnt(articleInfoVO.getArticleVisitCnt());
			articleInfo.setUserInfo(userInfo);
			articleInfo.setMapInfo(mapInfo);
			this.getiArticleinfoDao().add(articleInfo);
			
			//创建标签和文章的关系实例
			for(int i=0;i<tagInfoVOList.size();i++) {
				tagInfo = this.getiTaginfoDao().findById(tagInfoVOList.get(i).getTagId());
				tagArticleRelId = new TagArticleRelId();
				tagArticleRelId.setTagId(tagInfo.getTagId());
				tagArticleRelId.setArticleId(articleInfo.getArticleId());
				tagArticleRel = new TagArticleRel();
				tagArticleRel.setId(tagArticleRelId);
				tagArticleRel.setTagInfo(tagInfo);
				tagArticleRel.setArticleInfo(articleInfo);
				this.getiTagArticleRelDao().add(tagArticleRel);
			}
			resultDtoAddArticle.setSuccess(true);
			return resultDtoAddArticle;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoAddArticle.setSuccess(false);
			return resultDtoAddArticle;
		}
	}
	
	/**
	 * 
	 * @author ZhongJiang
	 * 
	 * @function 更具Tag数组获取文章列表
	 * 
	 * @param Tag标签数组
	 * 
	 * @return {ResultDto<ArticleinfoVO>}
	 * 
	 */
	@Override
	public ResultDto<ArticleinfoVO> doQueryArticleListByTags(String[] tagArr) {
		logger.info("Hello - ArticleServiceImpl - doQueryArticleListByTags...");
		ResultDto<ArticleinfoVO> resultDto = new ResultDto<ArticleinfoVO>();
		ArticleinfoVO articleInfoVO = null;
		List<ArticleInfo> articleInfoList = null;
		ArticleInfo articleInfo = null;
		try {
			articleInfoList = this.getiArticleinfoDao().findArticleByTags(tagArr);
			if(articleInfoList.size() != 0) {
				for(int i=0;i<articleInfoList.size();i++) {
					articleInfo = articleInfoList.get(i);
					articleInfoVO = new ArticleinfoVO();
					articleInfoVO.setArticleId(articleInfo.getArticleId());
					articleInfoVO.setArticleTitle(articleInfo.getArticleTitle());
					articleInfoVO.setArticleSummary(articleInfo.getArticleSummary());
					articleInfoVO.setArticleTag(articleInfo.getArticleTag());
					articleInfoVO.setArticleContent(articleInfo.getArticleContent());
					articleInfoVO.setArticleCreateDttmStr(HoneyCombTools.formatDTTM(HoneyCombTools.convertIntToDTTM(articleInfo.getArticleCreateDttm())));
					articleInfoVO.setUserId(articleInfo.getUserInfo().getUserId());
					articleInfoVO.setUserNickname(articleInfo.getUserInfo().getUserNickname());
					articleInfoVO.setUserHeadImgUrl(articleInfo.getUserInfo().getUserHeadImgUrl());
					resultDto.addToResultList(articleInfoVO);
				}
				resultDto.setSuccess(true);
			}else {
				resultDto.setSuccess(false);
			}
			return resultDto;
		} catch (Exception e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
			return resultDto;
		}
	}
	
}









